package com.bolt.admin.module.log.controller;


import com.bolt.admin.logging.annotation.LogAnnotation;
import com.bolt.admin.module.log.dto.LogDTO;
import com.bolt.admin.module.log.dto.LogQueryCriteria;
import com.bolt.admin.module.log.dto.converter.LogConverter;
import com.bolt.admin.module.log.entity.LogEntity;
import com.bolt.admin.module.log.service.LogService;
import com.bolt.admin.security.shiro.AuthContextHolder;
import com.bolt.admin.security.shiro.DefaultAuthUserDetails;
import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2020-10-30 14:30:17
 *         系统日志
 **/
@RestController
@RequestMapping("/api/sys/log")
@RequiredArgsConstructor
@Api(description = "系统日志")
public class LogController {

    private final LogService logService;

    /**
     * 系统日志日志列表分页
     */
    @GetMapping
    @ApiOperation(notes = "系统日志分页", httpMethod = "GET", value = "系统日志日志列表分页")
    public ResultMessage list(Pageable pageable, LogQueryCriteria logQueryCriteria) {
        PageInfo<LogEntity> pageInfo = logService.findPage(pageable, (root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, logQueryCriteria, criteriaBuilder));
        return Results.success(pageInfo.map(K -> LogConverter.toDTO(K)));
    }

    @GetMapping("/user")
    public ResultMessage user(Pageable pageable, LogQueryCriteria logQueryCriteria) {
        DefaultAuthUserDetails defaultAuthUserDetails = (DefaultAuthUserDetails) AuthContextHolder.getAuthUserDetails();
        logQueryCriteria.setSuccess(true);
        logQueryCriteria.setBlurry(defaultAuthUserDetails.getUserName());
        PageInfo<LogEntity> pageInfo = logService.findPage(pageable, (root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, logQueryCriteria, criteriaBuilder));
        return Results.success(pageInfo.map(K -> LogConverter.toDTO(K)));
    }
    /**
     * 根据ID系统日志日志操作
     */
    @GetMapping("/get/{id}")
    @ApiOperation(notes = "根据ID查询系统日志日志", httpMethod = "GET", value = "根据ID查询系统日志日志")
    public ResultMessage<LogDTO> get(@PathVariable Long id) {
        LogEntity logEntity = logService.findOne(id).get();
        return Results.success(LogConverter.toDTO(logEntity));
    }

    @DeleteMapping(value = "/clearInfo")
    @LogAnnotation(module = "系统日志", description = "清空所有系统成功日志")
    @ApiOperation("清空所有系统成功日志")
    @RequiresPermissions("monitor:server")
    public ResultMessage delAllInfoLog() {
        logService.clear(true);
        return Results.success();
    }

    @DeleteMapping(value = "/clearError")
    @LogAnnotation(module = "系统日志", description = "清空所有系统错识日志")
    @ApiOperation("清空所有系统错识日志")
    @RequiresPermissions("monitor:server")
    public ResultMessage delAllErrorLog() {
        logService.clear(false);
        return Results.success();
    }

}